<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adlegalisir extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model("m_pengajuan");
	}
	public function json_pengajuan(){
		$this->load->model('addpengajuan');
		$pengajuan = $this->addpengajuan->get_join_pemohon();
		echo json_encode(array("aaData"=>$pengajuan));
	}
	public function coba(){
		$this->load->view("table");
	}
	public function permohonan()
	{	
		$this->load->model('addpengajuan');
		
		
		/* $page = $this->input->get('page');
		$itemPerPage = 5;
		
		$first=0;
		if($page != null){
			$first = ($itemPerPage * ($page-1));
		} */
	$data['title'] = '| Semua Permohonan';
		$data['users'] = $this->addpengajuan->get_join_pemohon();
		// $data['total'] = count($data['users']);
		$data['main']= 'admin/legalisir/permohonan/v_adPermohonan';
		$this->load->view('v_template',$data);
		// print_r($data['users']);
	}

	public function lihatPermohonan($id_pengajuan = NULL)
	{
		$this->load->model('addpengajuan');
		
		//kalau id_pengajuan kosong, maka data diset NULL
		if(!isset($id_pengajuan))
		{
			$data['users'] = NULL;
		}
		else
		{
			$data['users'] = $this->addpengajuan->selectUserByID($id_pengajuan);
		}
		$data['title']	= ' | Lihat Permohonan';
		$data['main']	= 'admin/legalisir/permohonan/v_adLihatPermohonan';
		$data["id"]		= $id_pengajuan;
		$this->load->view('v_template',$data);
	}

	public function persetujuan($jenis = FALSE)
	{
		$join	= array(
					array("table"=>"pengajuan","join_key"=>"ID_PEMOHON","join_table"=>"pemohon")
				);
		//Kedepannya harus tidak pakai ini
		$page = $this->input->get('page');
		$itemPerPage = 5;
		$first=0;
		if($page != null){
			$first = ($itemPerPage * ($page-1));
		}
		//End
		if($jenis == FALSE)
			$data["get"]	= $this->m_pengajuan->get_join($join,array("STATUS"=>($this->session->userdata('role') == 3) ? 2 : 3),FALSE);
		else
			$data["get"]	= $this->m_pengajuan->get_join($join,array("STATUS > "=>2),FALSE);
		
		$data['title']	= '| Data Persetujuan';
		$data['total']	= count($data["get"]);
		$data['main']	= 'admin/legalisir/persetujuan/v_adPersetujuan';
		$this->load->view('v_template',$data);
	}	
	
	public function verifikasiPengajuan($id_pengajuan){
		// status jadi 2, tanggal verifikasi, kirim email kadin
		$this->load->model('addpengajuan');
		$this->load->model('frontmodel');
		$this->addpengajuan->verifikasiPengajuan($id_pengajuan);
		$config = Array(
				'protocol' 		=> 'smtp',
				'smtp_host' 	=> 'ssl://smtp.googlemail.com',
				'smtp_port' 	=> 465,
				'smtp_user' 	=> 'dobolpol@gmail.com',
				'smtp_pass' 	=> 'dobolpolnjedug87',
				'mailtype'  	=> 'html', 
				'smtp_timeout'	=> 30,
				'charset'   	=> 'iso-8859-1'
		);

		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");

		$this->email->from('dobolpol@gmail.com', 'allen');

		$email_kadin = $this->frontmodel->getEmailByJabatan(1);
		$this->email->to($email_kadin);

		$this->email->subject('[Persetujuan Legalisir] ');
		$message = 'Terdapat permohonan legalisir.<br/>';
		$message .= 'Klik pada link berikut untuk melakukan persetujuan legalisir: ' . anchor(base_url('admin/adLegalisir/persetujuanLegalisir/'.$id_pengajuan),'Setujui Legalisir');

		// $this->email->message('http://localhost:8787/legalisironline/front/activation'); 
		$this->email->message($message);		

		$result = $this->email->send();

		$this->load->view('admin/legalisir/persetujuan/v_persetujuanLegalisir');
	}

	public function verifikasiLegalisir($id){
		// status jadi 3, tanggal verifikasi
		$this->load->model('addpengajuan');
		$this->addpengajuan->verifikasiLegalisir($id);
		$this->load->view('admin/legalisir/persetujuan/v_persetujuanLegalisir'); 
	}

	public function persetujuanLegalisir($id)
	{
		// status jadi 3, tanggal persetujuan
		//$data['main']= 'admin/legalisir/persetujuan/v_persetujuanLegalisir';
		$this->load->model('addpengajuan');
		$this->addpengajuan->persetujuanLegalisir($id);
		$this->load->view('admin/legalisir/persetujuan/v_persetujuanLegalisir');
	}

	public function lihatPersetujuan($id = NULL)
	{
		
		$this->load->library("form_validation");
		if($this->session->userdata("role") == 4)
			$this->form_validation->set_rules("ket","Keterangan","required");
		if($this->form_validation->run() == false){
			$join	= array(
						array("table"=>"pengajuan","join_key"=>"ID_PEMOHON","join_table"=>"pemohon"),
						array("table"=>"pemohon","join_key"=>"ID_USER","join_table"=>"user")
					);
			$data["id"]		= $id;
			$column	= array("NAMA_LENGKAP","TEMPAT_LAHIR","TGL_LAHIR","JENIS_KELAMIN","ALAMAT","TELP","HP","EMAIL","ALAMAT_KTP","LEMBAR_TK1","TK1","LEMBAR_TK2","TK2","LEMBAR_TK3","TK3","ALAMAT_TK1","ALAMAT_TK2","ALAMAT_TK3","ALASAN","KETERANGAN","pengajuan.STATUS","TGL_PENGAJUAN","TGL_CETAK","TGL_CETAK2","TGL_CETAK3");			
			if($id != NULL)
			{
				$data["row"]	= $this->m_pengajuan->get_join($join,array("ID_PENGAJUAN"=>$id),TRUE,$column);
			}
			else
			{
				$data["row"]	= NULL;
			}
			$data['title']	= '| Lihat Persetujuan';
			$data['main']	= 'admin/legalisir/persetujuan/v_adLihatPersetujuan';
			$this->load->view('v_template',$data);
		} else {
			$ket	= $this->input->post("ket");
			$this->m_pengajuan->save(array("KETERANGAN"=>$ket,"STATUS"=>4,"TGL_CETAK"=>date("Y-m-d")),$id);
			redirect(base_url("admin/adlegalisir/persetujuan"));
		}
	}
	public function v_image($id,$jenis){
		$join	= array(
					array("table"=>"pengajuan","join_key"=>"ID_PEMOHON","join_table"=>"pemohon"),
					array("table"=>"pemohon","join_key"=>"ID_USER","join_table"=>"user")
				);
		$data["id"]		= $id;
		$data["jenis"]	= $jenis;
		$data["row"]	= $this->m_pengajuan->get_join($join,array("ID_PENGAJUAN"=>$id),TRUE);
		$this->load->view('admin/legalisir/persetujuan/v_image',$data);
	}
	public function cetak()
	{
		$data['main']= 'admin/legalisir/cetak/v_adCetak';
		$this->load->view('v_template',$data);
	}
	public function image(){
		$path	= $this->input->get("path");
		$source_file = $_SERVER['DOCUMENT_ROOT'] . "/legalisironline/" . $path;
		// echo $source_file;
		$im = ImageCreateFromJpeg($source_file); 

		$imgw = imagesx($im);
		$imgh = imagesy($im);
		
		for ($i=0; $i<$imgw; $i++)
		{
				for ($j=0; $j<$imgh; $j++)
				{
				
						// get the rgb value for current pixel
						
						$rgb = ImageColorAt($im, $i, $j); 
						
						// extract each value for r, g, b
						
						$rr = ($rgb >> 16) & 0xFF;
						$gg = ($rgb >> 8) & 0xFF;
						$bb = $rgb & 0xFF;
						
						// get the Value from the RGB value
						
						$g = round(($rr + $gg + $bb) / 3);
						
						// grayscale values have r=g=b=g
						
						$val = imagecolorallocate($im, $g, $g, $g);
						
						// set the gray value
						
						imagesetpixel ($im, $i, $j, $val);
				}
		}

		header('Content-type: image/jpeg');
		imagejpeg($im);
		// echo "te";
		// echo $source_file;
	}
	public function pencarian()
	{
		$data['title']	= '| Data Pencarian';
		$data['main']	= 'admin/legalisir/pencarian/v_adPencarian';
		$this->load->view('v_template',$data);
	}
	
	public function lihatPencarian()
	{
		$data['main']= 'admin/legalisir/pencarian/v_adLihatPencarian';
		$this->load->view('v_template',$data);
	}

	// start jenis permohonan baru dan lama
	
	public function j_permohonan($id_status = NULL)
	{
		$this->load->model("addpengajuan");
		
		if($id_status == 1)
		{		
			$page 			= $this->input->get('page');
			$itemPerPage 	= 5;
			$first			= 0;
			if($page != null)
			{
				$first = ($itemPerPage * ($page-1));
			}
			
			$data['title']	= " | Permohonan Baru";
			$data['users']  = $this->addpengajuan->get_join_pemohon_baru();
			$data['total']	= $this->addpengajuan->getTotalRecords_baru($id_status);
			$data['main']	= 'admin/legalisir/permohonan/v_j_permohonan';
			$this->load->view('v_template',$data);
		}
		elseif($id_status == 2)
		{
			$page = $this->input->get('page');
			$itemPerPage = 5;
			
			$first=0;
			if($page != null){
				$first = ($itemPerPage * ($page-1));
			}
			$data['title'] = " | Permohonan Lama";
			$data['users']  = $this->addpengajuan->get_join_pemohon_lama();
			$data['total'] = $this->addpengajuan->getTotalRecords_lama($id_status);
			$data['main']= 'admin/legalisir/permohonan/v_j_permohonan';
			$this->load->view('v_template',$data);
		}
	}
	
	public function tolak_permohonan($id = NULL)
	{
		$data = array(
			"STATUS"  => 0
		);
	
		$this->m_pengajuan->save($data,$id);
		redirect("admin/adlegalisir/j_permohonan/1");
	}
	
	public function konfirmasi_permohonan($id = NULL)
	{
		$data = $this->m_pengajuan->get($id);
		
		if($data['VERIFIKATOR_N'] == NULL)
		{
			if($this->m_pengajuan->save(array("VERIFIKATOR_N"	=> 'pending'),$id)){
				echo "pending";
			} else {
				echo "error";
			}	
		}
		else
		{
			if($this->m_pengajuan->save(array("STATUS"	=> 2,"VERIFIKATOR_N" => 'sent'),$id)){
				echo "success";
			} else {
				echo "error";
			}
		}
		
		
	}
	
	public function konf_sekre()
	{
		$id			= $this->input->get("id");
		$nama		= $this->input->get("nama");
		$alamat		= $this->input->get("alamat");
		$sd			= $this->input->get("sd");
		$smp		= $this->input->get("smp");
		$sma		= $this->input->get("sma");
		$korektor	= $this->input->get("korektor");
		
		$data	= array(
			"TGL_VERIFIKASI"	=> date('Y-m-d')
		);
		
		$this->m_pengajuan->save($data,$id);
		redirect('http://yeavirtual.com/index.php/mapi/konfirmasi_kadin?nama=' . $nama .'&alamat=' . $alamat .'&sd=' . $sd .'&smp=' . $smp .'&sma=' . $sma . '&korektor=' . $korektor .'&id='.$id,'refresh');
	}
	
	public function konf_akhir_sekre()
	{
		$id		= $this->input->get("id");
		
		$data	= array(
			"TGL_VERIFIKASI"	=> date('Y-m-d'),
			"STATUS"				=> 2
		);
		$this->m_pengajuan->save($data,$id);
		redirect('http://http://202.154.13.119/legalisironline/front/redirect_approve');
	}
	
	public function konf_akhir_kadin()
	{
		$id		= $this->input->get("id");
		
		$data	= array(
			"TGL_VERIFIKASI_KADIN"	=> date('Y-m-d'),
			"STATUS"				=> 2
		);
		$this->m_pengajuan->save($data,$id);
		redirect('http://202.154.13.119/legalisironline/front/redirect_approve');
	}
	
	public function sudah_dicetak($id,$jenis)
	{
		$this->load->model("m_pengajuan");
		$query  = $this->m_pengajuan->get($id);
		$status = 2;
		$query2	= $this->m_pengajuan->get_by(array("TGL_CETAK"=>date("Y-m-d"),"TGL_CETAK2"=>date("Y-m-d"),"TGL_CETAK3"=>date("Y-m-d")), FALSE, TRUE, TRUE, FALSE, "NOREG DESC");
		if(empty($query2))
			$noreg	= 1;
		else
			$noreg	= $query2["NOREG"] + 1;
		$undone	= 0;
		for($a=1;$a<=3;$a++){
			$no	= $a == 1 ? "" : $a;
			if($query["LEMBAR_TK".$a] > 0){
				if(is_null($query["TGL_CETAK".$no])){
					$undone++;
				}	
			}
		}
		if($undone == 1){
			$status = 3;
		}
		if(is_null($query["NOREG"])){
			$data = array(
				"STATUS"	=> $status,
				($jenis == 1) ? ("TGL_CETAK") : (($jenis == 2) ? ("TGL_CETAK2") : ("TGL_CETAK3")) => date("Y-m-d"),
				"NOREG" => $noreg
			);
		} else {
			$data = array(
				"STATUS"	=> $status,
				($jenis == 1) ? ("TGL_CETAK") : (($jenis == 2) ? ("TGL_CETAK2") : ("TGL_CETAK3")) => date("Y-m-d")
			);
		}
		
		if($this->m_pengajuan->save($data,$id))
		{
			if($undone == 1){
				echo "sending";
				// echo $undone;
			} else {
				echo "success";
				// echo $undone;
			}
		}
		else
		{
			echo "error";
		}
	}
	
	public function ubah_status($id = NULL, $id_ = NULL)
	{	
		if(!isset($id))
		{
			$this->load->model('addpengajuan');
		
		
			$page = $this->input->get('page');
			$itemPerPage = 5;
			
			$first=0;
			if($page != null){
				$first = ($itemPerPage * ($page-1));
			}
			
			$data['title']	= '| Ubah Status Berkas';
			$data['users'] 	= $this->addpengajuan->get_join_pemohon();
			$data['total'] 	= count($data['users']);
			$data['main']	= 'admin/legalisir/permohonan/v_ubahstatus';
			$this->load->view('v_template',$data);
		}
		else
		{
			$this->load->helper(array('form', 'url'));
			$this->load->library('form_validation');
			
			$this->form_validation->set_rules("id_pengajuan","ID Pengajuan","required");
			
			if($this->form_validation->run() == FALSE)
			{
				echo"
					<link rel='stylesheet' href='".base_url()."css/bootstrap.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."css/animate.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."css/font-awesome.min.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."css/font.css' type='text/css' cache='false' />
					<link rel='stylesheet' href='".base_url()."js/select2/select2.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."js/fuelux/fuelux.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."js/datepicker/datepicker.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."css/plugin.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."css/app.css' type='text/css' />
					<link rel='stylesheet' href='".base_url()."js/datatables/datatables.css' type='text/css' />
					<header class='bg-success panel-heading font-bold'>
						Permohonan Legalisir
					</header>
					<div class='row text-sm wrapper'>
						<div class='col-sm-3'>
							<div class='input-group'>
								<input type='text' value='$id_' style='display:none;'>
								<select name='status class='select btn-group'>
									<option value=''>-- Pilih Status --</option>
									<option value='1'>Pengajuan Awal</option>
									<option value='2'>Persetujuan Sekretaris / Kadin</option>
									<option value='3'>Telah Dicetak</option>
								</select>
							</div>
						</div>
					</div>
				";
			}
			else
			{
				echo "simpan";
			}
		}
	}
	function get_ttd(){
		if($this->session->userdata("role") == 3){
			$url	= $_GET["url"];
			$file	= $_SERVER['DOCUMENT_ROOT']."/legalisironline/496026dc/" . $url;
			if(file_exists($file)){
				header('Content-Type:' . mime_content_type($file));
				header('Content-Length: ' . filesize($file));
				readfile($file);
			}
		}
	}
	
}

/* End of file adLegalisir.php */
/* Location: ./application/controllers/admin/adLegalisir.php */